
import time
import matplotlib.pyplot as plt

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET


def get_depart_time(filename):
    depart_time = []
    for event, elem in ET.iterparse(filename):
        if event == 'end':
            if elem.tag == 'vehicle':
                depart_time.append(elem.attrib['depart'])
        elem.clear()
    return depart_time


def write_to_file(depart_time, filename):
    fp = open(filename, mode='a')
    for item in depart_time:
        fp.write(item)
        fp.write('\n')
    fp.close()


def split(line):
    print line.strip('\n')
    return int(float(line))


def test_split():
    line = '19245.00\n'
    print 'test split 19245.00, after split function: {0}'.format(split(line))


def read_file(filename, split):
    fp = open(filename, 'rb')
    data = []
    for line in fp.readlines():
        elements = split(line)
        data.append(elements)
    return data


def statistics(filename):
    depart_time = None
    if depart_time is None and filename is not None:
        depart_time = read_file(filename, split)
    result = dict()
    for item in depart_time:
        result.setdefault(item, 0)
        result[item] += 1
    return sorted(result.iteritems(), key=lambda d: d[0], reverse=False)


def count_ten_minutes(filename):
    depart_time = read_file(filename, split)
    result = dict()
    for item in depart_time:
        # recalculate the time window
        key = item / 600
        result.setdefault(key, 0)
        result[key] += 1
    return sorted(result.iteritems(), key=lambda d: d[0], reverse=False)


def plot_bar(scatter):
    X = []
    y = []
    for a, b in scatter:
        X.append(a)
        y.append(b)

    plt.scatter(X, y)
    plt.show()


def test_plot_bar():
    scatter = [(1, 2), (3, 4), (5, 6)]
    plot_bar(scatter)


def main():
    start_time = time.clock()
    depart_time = get_depart_time('../../data/LuSTScenario.internal.mobility.033.rou.xml')
    end_time = time.clock()
    print 'Parsing XML using {0} seconds'.format(end_time - start_time)
    start_time = time.clock()
    write_to_file(depart_time, '../../data/LuSTScenario.internal.mobility.033.depart.txt')
    end_time = time.clock()
    print 'Writing data to file using {0} seconds.'.format(end_time - start_time)
    pass


def aggregation():
    # file_list = ['../../data/LuSTScenario.internal.mobility.033.rou.xml',\
    #              '../../data/LuSTScenario.internal.mobility.066.rou.xml',\
    #              '../../data/LuSTScenario.internal.mobility.100.rou.xml']
    file_list = ['../../data/hangzhou08.rou.xml']
    # save_file = '../../data/LuSTScenario.internal.mobility.agg.depart.txt'
    save_file = '../../data/hangzhou08.depart.txt'
    for filename in file_list:
        print 'parsing {0}'.format(filename)
        depart_time = get_depart_time(filename)
        write_to_file(depart_time, save_file)

if '__main__' == __name__:
    # test_split()
    # main()
    aggregation()
